package demo7.teht;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JApplet;
import javax.swing.JPanel;

/**
 *
 * @author Pasi
 */
public class T5_Balls extends JApplet implements Runnable {
    //private Ball ball;
    private Thread saie;
    private int BALLS = 1000;
    
    private ArrayList<T5_Ball> balls = new ArrayList<>();
    
    @Override
    public void init() {
        //this.ball = new Ball(getSize().width,getSize().height);
        for (int i=1;i<=BALLS;i++){
            balls.add(new T5_Ball(getSize().width,getSize().height));
        }
        getContentPane().add(new PiirtoPaneeli()); 
    }
    
    @Override
    public void start() {
        if (saie == null) {
            saie = new Thread(this);
            saie.start();
        }
    }
    
    @Override
    public void run() {
        while (true) {
            for (int i=0;i<BALLS;i++){
                (balls.get(i)).move();
            }
            repaint();
            try {
                saie.sleep(10);
            } catch (InterruptedException e) {}
        }
    }
    
    class PiirtoPaneeli extends JPanel {
       @Override
       public void paintComponent(Graphics g) {
           super.paintComponent(g);
           // g.fillOval(ball.getX(), ball.getY(), ball.getSize(), ball.getSize());
           for (int i=0;i<BALLS;i++){
               T5_Ball ball = balls.get(i);
               g.setColor(ball.getColor());
               g.fillOval(ball.getX(), ball.getY(), ball.getSize(), ball.getSize());
           }
       }
   }
}
